import MySQL
import Visualization


def draw(display_group=None):
    labels = {}
    if display_group is None:
        display_group = []
        for c in range(0, 26):
            display_group.append(chr(ord('a') + c))
    for k in range(len(display_group)):
        labels[display_group[k]] = k
    sql = 'SELECT * FROM project_count WHERE group_len=1 AND date<\'20160108\''
    rows = MySQL.query(sql)
    maps = {}
    for row in rows:
        date = row['date']
        group = row['group']
        # count = row['count']
        proportion = row['proportion']
        if group in labels:
            if date in maps:
                maps[date]['x'][labels[group]] = group
                maps[date]['y'][labels[group]] = proportion
            else:
                maps[date] = {'x': [None] * len(labels), 'y': [None] * len(labels)}
                maps[date]['x'][labels[group]] = group
                maps[date]['y'][labels[group]] = proportion
    Visualization.draw(maps)


def draw_hour(display_group=None):
    labels = {}
    if display_group is None:
        display_group = []
        for c in range(0, 26):
            display_group.append(chr(ord('a') + c))
    for k in range(len(display_group)):
        labels[display_group[k]] = k
    sql = 'SELECT * FROM hour_count WHERE date<\'20160103\''
    rows = MySQL.query(sql)
    maps = {}
    for row in rows:
        date = row['date']
        group = row['group']
        hour = row['hour']
        # count = row['count']
        proportion = row['proportion']
        if group in labels:
            key = date + '_' + '%02d' % hour
            if key in maps:
                maps[key]['x'][labels[group]] = group
                maps[key]['y'][labels[group]] = proportion
            else:
                maps[key] = {'x': [None] * len(labels), 'y': [None] * len(labels)}
                maps[key]['x'][labels[group]] = group
                maps[key]['y'][labels[group]] = proportion
    Visualization.draw(maps, legend=False, title='Wiki Page Count in 48h')


def draw_hour_longer(display_group=None):
    labels = {}
    if display_group is None:
        display_group = []
        for c in range(0, 26):
            display_group.append(chr(ord('a') + c))
    for k in range(len(display_group)):
        labels[display_group[k]] = k
    sql = 'SELECT * FROM hour_count ORDER BY id ASC'
    rows = MySQL.query(sql)
    sums = {}
    for i in range(len(rows)):
        key = rows[i]['date'] + '_' + str(rows[i]['hour'])
        if key not in sums:
            sums[key] = 0
        sums[key] += rows[i]['count']
    for i in range(len(rows)):
        key = rows[i]['date'] + '_' + str(rows[i]['hour'])
        rows[i]['proportion'] = rows[i]['count'] * 1.0 / sums[key]
    maps = {}
    for row in rows:
        date = row['date']
        group = row['group']
        hour = row['hour']
        proportion = row['proportion']
        if group in labels:
            key = ('%02d' % (int(date) - 20160100)) + '_' + ('%02d' % hour)
            if group not in maps:
                maps[group] = {'x': [None] * (24 * 30), 'y': [None] * (24 * 30)}
            maps[group]['x'][24 * (int(date) - 20160101) + hour] = key
            maps[group]['y'][24 * (int(date) - 20160101) + hour] = proportion
    Visualization.draw(maps, chart_type='line',
                       legend=True, title='Wiki Page Count in 30 days', x_label='time line (20160101 - 20160130)')


def draw_hour_sum(display_group=None):
    labels = {}
    if display_group is None:
        display_group = []
        for c in range(0, 26):
            display_group.append(chr(ord('a') + c))
    for k in range(len(display_group)):
        labels[display_group[k]] = k
    sql = 'SELECT * FROM hour_count WHERE date<\'20160103\''
    rows = MySQL.query(sql)
    maps = {}
    for row in rows:
        date = row['date']
        group = row['group']
        hour = row['hour']
        # count = row['count']
        proportion = row['sum_proportion']
        if group in labels:
            key = date + '_' + '%02d' % hour
            if key in maps:
                maps[key]['x'][labels[group]] = group
                maps[key]['y'][labels[group]] = proportion
            else:
                maps[key] = {'x': [None] * len(labels), 'y': [None] * len(labels)}
                maps[key]['x'][labels[group]] = group
                maps[key]['y'][labels[group]] = proportion
    Visualization.draw(maps, save="C:\\Users\\sissors-lab\\PycharmProjects\\AQPExperiment\\figure\\wiki\\1.svg", legend=False, title='Wiki Page Count in 48h')


def draw_hour_sum_longer(display_group=None):
    labels = {}
    if display_group is None:
        display_group = []
        for c in range(0, 26):
            display_group.append(chr(ord('a') + c))
    for k in range(len(display_group)):
        labels[display_group[k]] = k
    sql = 'SELECT * FROM hour_count ORDER BY id ASC'
    rows = MySQL.query(sql)
    for i in range(len(rows)):
        tmp = 0
        for j in range(i + 1):
            if rows[j]['group'] == rows[i]['group']:
                tmp += rows[j]['count']
        rows[i]['sum_count'] = tmp
    sums = {}
    for i in range(len(rows)):
        key = rows[i]['date'] + '_' + str(rows[i]['hour'])
        if key not in sums:
            sums[key] = 0
        sums[key] += rows[i]['sum_count']
    for i in range(len(rows)):
        key = rows[i]['date'] + '_' + str(rows[i]['hour'])
        rows[i]['sum_proportion'] = rows[i]['sum_count'] * 1.0 / sums[key]
    maps = {}
    for row in rows:
        date = row['date']
        group = row['group']
        hour = row['hour']
        proportion = row['sum_proportion']
        if group in labels:
            key = ('%02d' % (int(date) - 20160100)) + '_' + ('%02d' % hour)
            if group not in maps:
                maps[group] = {'x': [None] * (24 * 30), 'y': [None] * (24 * 30)}
            maps[group]['x'][24 * (int(date) - 20160101) + hour] = key
            maps[group]['y'][24 * (int(date) - 20160101) + hour] = proportion
    Visualization.draw(maps, chart_type='line', legend=True, title='Wiki Page Count in 30 days', x_label='time line (20160101 - 20160130)')



if __name__ == '__main__':
    MySQL.init('wiki')
    # draw(['c', 'd', 'e', 'f', 'i', 'j', 'p', 'r', 'z'])
    # draw(['a', 'b', 'z'])
    # draw()
    # draw_hour(['d', 'e', 'f', 'i', 'j', 'p', 'r', 'z'])
    # draw_hour()
    # draw_hour_sum(['d', 'e', 'f', 'i', 'j', 'p', 'r', 'z'])
    draw_hour_longer(['e', 'i', 'j', 'r'])
    # draw_hour_sum_longer(['e', 'i', 'j', 'r'])
    # draw_hour_sum_longer(['e'])